<template>
  <div>
    <el-row style="border: 1px solid rgba(0, 123, 255, 0.2);width:60%;margin-left:20%;">
      <el-col :span="24" style="border-bottom: 1px solid rgba(0, 123, 255, 0.2);padding:15px;text-align:center;background-color: rgba(0, 123, 255, 0.15);">
        <div class="grid-content bg-purple">豪车之家客服聊天系统</div>
      </el-col>
      <el-col :span="18">
        <div class="content-top">
          <!-- 消息区 -->
<!--  -->
          <!-- <div class="lite-chatbox" v-show="right">
            <div class="cright cmsg">
              <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;"  :src="'/lib/img/A.jpg'"/>
              <span class="name">SuperPaxxs</span>
              <span class="content">{{mysend}}</span>
            </div>
          </div> -->
          <div class="lite-chatbox" v-show="right1" id="right1">
              <div class="cleft cmsg">
                <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;"  :src="'/lib/img/B.jpg'"/>
                <span class="name"><span class="htitle admin">admin</span>客服1号</span>
                <span class="content"><span id="aaa"></span></span>
              </div>
          </div>
          <div class="lite-chatbox" v-show="right">
            <div class="cright cmsg">
              <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;"  :src="'/lib/img/A.jpg'"/>
              <span class="name">SuperPaxxs</span>
              <span class="content">{{mysend}}</span>
            </div>
          </div>
        </div>
        <!--  -->
          
        <div class="content-bottom">
          <textarea class="message" id="message" cols="30" rows="10" placeholder="在此输入文本消息..." v-model="message" @keyup.enter="sendMessage"></textarea>
        </div>
        <el-row id="btn">
          <el-button type="primary" plain @click="sendMessage">提交</el-button>
        </el-row>
      </el-col>
      <el-col :span="6" style="height:490px;border-left: 1px solid rgba(0, 123, 255, 0.2);">
        <div class="grid-content bg-purple-light">常见问题</div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data(){
    return {
      webs:"",
      mId:1,
      message:"",
      mysend:"",
      info:{},
      right:false,
      right1:false
    }
  },
  methods:{
    sendMessage(){
      let data = {
        adminId: this.mId,
        uId: 1,
        content: this.message
      }
      this.webs.send(JSON.stringify(data));
      this.mysend = this.message;
      // 
      this.right=true;
      this.webs.onmessage = function (evt) {
        // console.log(evt);
        let info = JSON.parse(evt.data);
        console.log(document.getElementById("aaa").innerHTML = info.content);
      }
      this.message="";
    },
    getConnention(){
      // 获取管理员id
      this.mId = JSON.parse(localStorage.getItem("adminInfo"))[0].a_id
      this.webs = new WebSocket('ws://localhost:80/koa/ws?id=' + this.mId);
      this.webs.onopen = function () {
        console.log('连接成功');
      }
      this.webs.onmessage = function (evt) {
        console.log(evt);
        let info = JSON.parse(evt.data);
        console.log(document.getElementById("aaa").innerHTML = info.content);
        document.getElementById("right1").style.display = "block"
        // console.log('连接==>'+evt.data);
      }
    }
  },
  created(){
    this.getConnention();
  },
  beforeDestroy(){
    this.onclose = function(){
      // 关闭 websocket
      alert("连接已关闭..."); 
    };
  }
}
</script>

<style scoped>
/* 主 */
 .lite-chatbox{
  font-family: Helvetica, "PingFang SC", "Microsoft YaHei", sans-serif;
  padding: 0px;
  font-size:18px;
  width: 100%;
  position:relative;
  overflow-y: auto;
  overflow-x: hidden;
}
/* 聊天区域 */
.lite-chatbox .cmsg{
  position: relative; 
  margin: 4px 7px;
  min-height: 50px;
  border: 0;
}
.lite-chatbox .cright{
  text-align: right;
  margin-left: 64px;
}
.lite-chatbox .cleft{
  text-align: left;
  margin-right: 64px;
}
/* 聊天组件：全局*/    
/* 头像 */
.lite-chatbox img.headIcon{
  width: 34px;
  height: 34px;
  top: 9px;
  position: absolute;
  border: 1px solid #c5d4c4;
}
/* 昵称 */
.lite-chatbox .name{
  color: #8b8b8b;
  font-size: 12px;
  display: block;
  line-height: 18px;
}
.lite-chatbox .name .htitle{
  display: inline-block;
  padding: 0 3px 0 3px;
  background-color: #cccccc;
  color: #ffffff;
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
  border-radius: 4px;
  margin-right: 4px;
  font-size: 11px;
  overflow: hidden;
  text-overflow:ellipsis;
  white-space: nowrap;
  vertical-align: middle;
  max-width: 50px;
}
/* 消息内容 */
.lite-chatbox .content{
  word-break: break-all;
  word-wrap: break-word;
  text-align: left;
  position: relative;
  display: inline-block;
  font-size: 15px;
  padding: 10px 15px;
  line-height: 20px;
  /* -moz-border-radius: 6px; 
  -webkit-border-radius: 6px; 
  border-radius: 6px; */
  /*空白消息*/
  min-width: 9px; 
  min-height: 18px;
}
/*消息内容图片处理*/
.lite-chatbox .content img{
  width: 100%;
  height: auto;
}
/*消息内容超链接处理*/
.lite-chatbox .content a{
  color: #0072C1;
  margin: 0 5px;
  cursor: hand;
}
/*聊天提示条*/
.lite-chatbox .tips{
  margin: 12px;
  text-align: center;
  font-size: 12px;
}
/*聊天提示条文本*/
.lite-chatbox .tips span{
  display: inline-block;
  padding: 4px;
  background-color: #cccccc;
  color: #ffffff;
  -moz-border-radius: 6px; 
  -webkit-border-radius: 6px; 
  border-radius: 6px;
}
/* 聊天组件：微调 */
/* 圆头像 */
.lite-chatbox img.radius{
  -moz-border-radius: 100%; 
  -webkit-border-radius: 100%; 
  border-radius: 100%;
}
/* 左右头像 */
.lite-chatbox .cright img.headIcon{
  right: 0;
}
.lite-chatbox .cleft img.headIcon{
  left: 0;
}
/* 左右昵称 */
.lite-chatbox .cright .name{
  margin: 0 48px 2px 0;
}
.lite-chatbox .cleft .name{
  margin: 0 0 2px 48px;
}
/* 左右聊天气泡 */
.lite-chatbox .cright .content{
  margin: 0 48px 0 0;
  -webkit-border-radius: 20px 0 20px 20px;
  border-radius: 20px 0 20px 20px;
  color: white;
  /* border: 1px solid #78cdf8; */
  background: -webkit-linear-gradient(70deg, #3FD1E1 0%, #44D7CD 100%);
  background: linear-gradient(20deg, #3f8fe1cc 0%, #44d7c9 100%);
  -webkit-box-shadow: 5px 5px 15px 0 rgba(102,102,102,0.15);
  box-shadow: 5px 5px 15px 0 rgba(102,102,102,0.15);
}
.lite-chatbox .cleft .content{
  margin: 0 0 0 48px;
  -webkit-border-radius: 0 20px 20px 20px;
  border-radius: 0 20px 20px 20px;
  color: #666;
  border: 1px solid rgba(0, 0, 0, 0.05);
  background: #FFFFFF;
  -webkit-box-shadow: 5px 5px 15px 0 rgba(102,102,102,0.1);
  box-shadow: 5px 5px 15px 0 rgba(102,102,102,0.1);
}
.lite-chatbox .cright .content:after{
  right: -12px;
  top: 8px;
}
.lite-chatbox .cleft .content:after{
  left: -12px;
  top: 8px;
}
/* 聊天提示条附加样式 */
.lite-chatbox .tips .tips-primary{
  background-color: #3986c8;
}
.lite-chatbox .tips .tips-success{
  background-color: #49b649;
}
.lite-chatbox .tips .tips-info{
  background-color: #5bb6d1;
}
.lite-chatbox .tips .tips-warning{
  background-color: #eea948;
}
.lite-chatbox .tips .tips-danger{
  background-color: #e24d48;
}
.lite-chatbox .name .admin{
  background-color: #72D6A0;
}
.lite-chatbox .name .owner{
  background-color: #F2BF25;
}



.content-top{
    height: 100%;
    min-height: 319px;
  }
  .content-bottom{
    height: 150px;
    min-height:150px;
    width: 98.5%;
  }
  .question{
    height: 50px;
    border-bottom: 1px solid rgba(86, 61, 124, .2);
    line-height: 50px;
    padding-left: 20px;
  }
  .clearmp{
    margin: 0;
    padding: 0;
  }
  .message{
    width: 100%;
    height: 100%;
    border: none;
    resize: none;
    padding: 10px 5px;
    background-color: #E6E2EB;
  }
  #message:focus{
    outline: none;
  }
  #btn{
    position: absolute;
    right: 25%;
    bottom: 0%;
  }
</style>